package com.fenxiangtech.mall.web.interceptor;

import com.fenxiangtech.mall.enums.ScopeEnum;
import com.fenxiangtech.mall.exception.ForbiddenAccessException;
import com.fenxiangtech.mall.service.UserTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhangjiashun
 */
public class PrimaryScopeHandlerInterceptor implements HandlerInterceptor {

    @Autowired
    private UserTokenService userTokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object o) throws Exception {
        int scope = (int) userTokenService.getCurrentTokenVar("scope");
        if (scope < ScopeEnum.USER.getCode()) {
            throw new ForbiddenAccessException();
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
